﻿using MyTest;
using System;
using System.Collections.Generic;

using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Collection : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        #region 关联性泛型集合 <Key-Value>
        #region Dictionary哈希表
        Dictionary<string, Product2> dictProducts = new Dictionary<string, Product2>();
        dictProducts.Add("t1",new Product2());
        dictProducts.Add("t2", new Product2());
        dictProducts.Add("t3", new Product2());
        Response.Write("字典集合大小:" + dictProducts.Count + "<br>");
        Product2 p;
       Response.Write( dictProducts.TryGetValue("t1",out p) + "<br>");
        StringBuilder sb = new StringBuilder();
        //dictProducts.Values;//字典值的集合
        foreach (var key in dictProducts.Keys)
        {
            sb.Append(key + "\t");
        }
        Response.Write(sb.ToString() + "<br>");
        #endregion

        

        #region SortedDictionary(二叉树)
        SortedDictionary<string, Product2> sDictProducts = new SortedDictionary<string, Product2>();
        sDictProducts.Add("t1", new Product2());
        sDictProducts.Add("t2", new Product2());
        sDictProducts.Add("t3", new Product2());
        sDictProducts.Add("t4", new Product2());
        Response.Write("字典集合大小:" + sDictProducts.Count + "<br>");
        Response.Write(sDictProducts.ContainsKey("t10"));
        StringBuilder sb2 = new StringBuilder();
        foreach (var key in sDictProducts.Keys)
        {
            sb2.Append(key + "\t");

        }
        Response.Write(sb2.ToString() + "<br>");


        #endregion

        #region SortedList(数组)
        SortedList<string, Product2> sListProducts = new SortedList<string, Product2>();
        sListProducts.Add("t3",new Product2());
        sListProducts.Add("t2", new Product2());
        sListProducts.Add("t1", new Product2());
        StringBuilder sb3 = new StringBuilder();
        foreach (var key in sListProducts.Keys)
        {
            sb3.Append(key + "\t");

        }
        Response.Write(sb3.ToString() + "<br>");

        #endregion
        #endregion

        #region 非关联性泛型集合

        #region  List(数组)
        List<Product2> lProducts = new List<Product2>();
        lProducts.Add(new Product2());
        Response.Write("集合元素个数" + lProducts.Count + "<br>");
        Response.Write("第一个元素的hash值" + lProducts[0].GetHashCode() +"<br>");



        #endregion

        #region LinkedList(双向链表)
        LinkedList<Product2> llproducts = new LinkedList<Product2>();
        llproducts.AddLast(new Product2("aa", "type"));
         llproducts.AddLast(new Product2("bb", "type"));
        StringBuilder sb4 = new StringBuilder();
        foreach (var product in llproducts)
        {
            sb4.Append(product.Name + "\t");
}
        Response.Write(sb4.ToString() + "<br>");

        #endregion

        #region HashSet(哈希表) "相同的对象，判断两个对象相等"
        HashSet<Product2> hsProducts = new HashSet<Product2>();
        hsProducts.Add(new Product2("cc", "type"));
        hsProducts.Add(new Product2("aa", "type"));
        hsProducts.Add(new Product2("aa", "type1"));
        hsProducts.Add(new Product2("bb", "type"));
        StringBuilder sb5 = new StringBuilder();
        foreach (var product in hsProducts)
        {
            sb5.Append(product.Name + "\t");
        }
        Response.Write(sb.ToString() + "<br>");

        #endregion

        #region SortedSet(二叉树)
        SortedSet<Product2> ssproducts = new SortedSet<Product2>();
        ssproducts.Add(new Product2("cc", "type"));
        ssproducts.Add(new Product2("aa", "type"));
        ssproducts.Add(new Product2("aa", "type"));
        ssproducts.Add(new Product2("bb", "type"));
        StringBuilder sb6 = new StringBuilder();
        foreach (var product in ssproducts)
        {
            sb6.Append(product.Name + "\t");
}
        Response.Write(sb6.ToString() + "<br>");


        #endregion

        #region  Stack(栈)
        Stack<Product2> sproducts = new Stack<Product2>();
        sproducts.Push(new Product2("aa", "type"));
        sproducts.Push(new Product2("bb", "type"));
        Response.Write("栈顶元素:" + sproducts.Peek().Name + "<br>");
        Response.Write("栈内元素个数:" + sproducts.Count + "<br>");
        Response.Write("出栈: " + sproducts.Pop().Name + "<br>");
        Response.Write("栈顶元素: " + sproducts.Peek().Name + "<br>" );
        Response.Write("栈内元素个数 " + sproducts.Count + "<br>");

        #endregion

        #region Queue (队列)
        Queue<Product2> qPeoducts = new Queue<Product2>();
        qPeoducts.Enqueue(new Product2("aa","type"));
        qPeoducts.Enqueue(new Product2("bb","type"));
        Response.Write("队列头部元素：" + qPeoducts.Peek().Name + "<br>");
        Response.Write("队列元素个数" + qPeoducts.Count + "<br>");
        Response.Write("队列出队元素" + qPeoducts.Dequeue().Name + "<br>");
        Response.Write("队列头部元素：" + qPeoducts.Peek().Name + "<br>");
        Response.Write("队列元素个数" + qPeoducts.Count + "<br>");
        #endregion

        #endregion

    }
} 